package org.haredot.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 字符集编码过滤器
 */
//@WebFilter(value = "/*", initParams = {@WebInitParam(name="encoding", value = "UTF-8")},
//        dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.INCLUDE, DispatcherType.FORWARD})
public class CharacterEncodingFilter implements Filter {

    private String encoding ;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");

        if (encoding == null || "".equals(encoding)) {
            encoding = "UTF-8" ;
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        chain.doFilter(request, response);

    }
}
